<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL 压缩算法设计指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
            animation: float 20s linear infinite;
        }
        
        @keyframes float {
            0% { transform: translate(0, 0) rotate(0deg); }
            100% { transform: translate(-100px, -100px) rotate(360deg); }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            border-radius: 16px;
            padding: 30px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .feature-card::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            opacity: 0;
            transition: opacity 0.3s ease;
            z-index: -1;
        }
        
        .feature-card:hover::before {
            opacity: 1;
        }
        
        .feature-card:hover {
            transform: translateY(-5px);
            background: white;
        }
        
        .step-number {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 18px;
            flex-shrink: 0;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .pros-cons-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .pros-cons-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                URL 压缩算法
                <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">设计与实现指南</span>
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                将冗长的网址转化为简洁的标识符，探索高效的短链接生成技术
            </p>
            <div class="mt-10 flex justify-center gap-8">
                <div class="text-center">
                    <i class="fas fa-compress text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">智能压缩</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shield-alt text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">安全可靠</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-rocket text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">高性能</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                设计一个算法来压缩 URL 的目标是将长 URL 转换为较短的唯一标识符，并且能够在需要时将其还原为原始 URL。这个过程通常包括以下几个步骤：
            </p>
            
            <div class="grid md:grid-cols-3 gap-6 mt-10">
                <div class="feature-card">
                    <div class="flex items-start gap-4">
                        <div class="step-number">1</div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">生成唯一标识符</h3>
                            <p class="text-gray-600">创建一个唯一且短小的标识符来代替长 URL</p>
                        </div>
                    </div>
                </div>
                
                <div class="feature-card">
                    <div class="flex items-start gap-4">
                        <div class="step-number">2</div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">存储映射关系</h3>
                            <p class="text-gray-600">将生成的短标识符与原始 URL 之间的映射关系保存到数据库或内存中</p>
                        </div>
                    </div>
                </div>
                
                <div class="feature-card">
                    <div class="flex items-start gap-4">
                        <div class="step-number">3</div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">解码过程</h3>
                            <p class="text-gray-600">能够将短标识符解码回原始的长 URL</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Architecture Diagram -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8">系统架构图</h2>
            <div class="mermaid">
                graph TB
                    A[用户输入长URL] --> B{URL压缩系统}
                    B --> C[哈希算法]
                    B --> D[自增ID]
                    C --> E[生成短标识符]
                    D --> E
                    E --> F[存储映射关系]
                    F --> G[(数据库)]
                    H[用户访问短URL] --> I{解码系统}
                    I --> G
                    G --> J[返回原始URL]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#84fab0,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Design Details -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>设计短链接生成器
            </h2>
            <p class="text-lg text-gray-700 mb-8">
                生成唯一且短小的 URL 标识符，并且能够避免碰撞（即生成相同标识符的概率极低）。
            </p>

            <div class="space-y-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <span class="highlight-text">1.1 唯一标识符生成</span>
                    </h3>
                    <p class="text-gray-700 mb-6">常见的方法是使用哈希函数来生成短标识符，或者使用自增 ID 转换为短字符串。以下是常见的两种策略：</p>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-gradient-to-br from-purple-50 to-blue-50 rounded-xl p-6">
                            <h4 class="text-xl font-bold mb-3 flex items-center">
                                <i class="fas fa-hashtag mr-2 text-purple-600"></i>哈希方法
                            </h4>
                            <ul class="space-y-2 text-gray-700">
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>使用如 SHA-256 或 MD5 等哈希函数对长 URL 进行哈希</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                    <span>从哈希结果中提取前 N 位字符作为短标识符</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-exclamation-triangle text-yellow-500 mt-1 mr-2"></i>
                                    <span>这种方法有可能产生碰撞，但通过在数据库中维护映射关系可以解决</span>
                                </li>
                            </ul>
                        </div>
                        
                        <div class="bg-gradient-to-br from-blue-50 to-purple-50 rounded-xl p-6">
                            <h4 class="text-xl font-bold mb-3 flex items-center">
                                <i class="fas fa-sort-numeric-up mr-2 text-blue-600"></i>自增 ID 转换
                            </h4>
                            <ul class="space-y-2 text-gray-700">
                                <li class="flex items-start">